﻿using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace CS.Http
{
    /// <summary>
    /// Http请求辅助方法
    /// zhouyu,2011-08-11 created.
    /// zhouyu,2011-10-24 add async request method.
    /// zhouyu,2015-02-12 modify 超时均改为秒计算
    /// </summary>
    public class HttpHelper
    {


        #region 自动编码处理

        /// <summary>
        /// 获取经过自动解码后的页面，以防乱码
        /// <remarks></remarks>
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetHtml(string url)
        {
            var wc = new WebClient {Credentials = CredentialCache.DefaultCredentials};
            var htmlBuffer = wc.DownloadData(url);
            var regEx = new Regex("charset\\s*=\\s*[\\W]?\\s*([\\w-]+)", RegexOptions.IgnoreCase);
            var content = Encoding.Default.GetString(htmlBuffer);
            if (regEx.IsMatch(content))
            {
                var encoding = Encoding.GetEncoding(regEx.Match(content).Groups[1].Value.Trim());
                content = encoding.GetString(htmlBuffer);
            }
            else
            {
                content = Encoding.Default.GetString(htmlBuffer);
            }
            return content;
        }

        #endregion
        

        #region 同步处理

        /// <summary>
        /// Get方法请求并返回消息
        /// </summary>
        /// <param name="url">包含查询字符串的Url地址</param>
        /// <returns></returns>
        public static string Get(string url)
        {
            var sync = new SyncHttp();
            return sync.HttpGet(url);
        }

        /// <summary>
        /// 设定编码的
        /// </summary>
        /// <param name="url"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Get(string url,Encoding encoding)
        {
            var sync = new SyncHttp();
            sync.CurrEncoding = encoding;
            return sync.HttpGet(url);
        }

        /// <summary>
        /// Get方法请求并返回消息
        /// </summary>
        /// <param name="url">包含查询字符串的Url地址</param>
        /// <param name="timeout">超时，秒</param>
        /// <returns></returns>
        public static string Get(string url, int timeout)
        {
            var sync = new SyncHttp(timeout);
            return sync.HttpGet(url);
        }

        /// <summary>
        ///  Get方法请求并返回消息
        /// </summary>
        /// <param name="url"></param>
        /// <param name="timeout"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Get(string url, int timeout,Encoding encoding)
        {
            var sync = new SyncHttp(timeout) {CurrEncoding = encoding};
            return sync.HttpGet(url);
        }

        /// <summary>
        /// Post方法请求并返回消息 ,默认超时
        /// </summary>
        /// <param name="url">包含查询字符串的Url地址</param>
        /// <param name="urlParameters">参数集合</param>
        /// <returns></returns>
        public static string Post(string url, HttpParams urlParameters)
        {
            var sync = new SyncHttp();
            return sync.HttpPost(url, urlParameters);
        }

        /// <summary>
        /// Post方法请求并返回消息 ,可配超时
        /// </summary>
        /// <param name="url">包含查询字符串的Url地址</param>
        /// <param name="urlParameters">参数集合</param>
        /// <param name="timeout">超时，秒</param>
        /// <returns></returns>
        public static string Post(string url, HttpParams urlParameters, int timeout)
        {
            var sync = new SyncHttp(timeout);
            return sync.HttpPost(url, urlParameters);
        }
        /// <summary>
        /// Post方法请求并返回消息 ,可配超时
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static string Post(string url, string postData, int timeout)
        {
            var sync = new SyncHttp(timeout);
            return sync.HttpPost(url, postData);
        }



        #endregion


        #region 异步处理 Async


        /// <summary>
        /// 异步：Get方法请求并返回消息
        /// </summary>
        /// <param name="url"></param>
        /// <param name="timeout"></param>
        /// <param name="encoding">解码方式</param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static bool AsyncGet(string url, int timeout, Encoding encoding, AsyncHttpCallback callback)
        {
            var sync = new AsyncHttp(timeout) {CurrEncoding = encoding};
            return sync.HttpGet(url, callback);
        }


        /// <summary>
        /// 异步：Get方法请求并返回消息
        /// </summary>
        /// <param name="url">包含查询字符串的Url地址</param>
        /// <param name="timeout">超时，秒</param>
        /// <param name="callback">结束后的回调</param>
        /// <returns></returns>
        public static bool AsyncGet(string url, int timeout, AsyncHttpCallback callback)
        {
            var sync = new AsyncHttp(timeout);
            return sync.HttpGet(url, callback);
        }


        /// <summary>
        /// 异步：Post方法请求并返回消息 ,可配超时
        /// </summary>
        /// <param name="url">包含查询字符串的Url地址</param>
        /// <param name="urlParameters">参数集合</param>
        /// <param name="timeout">超时，秒</param>
        /// <param name="callback">结束后的回调</param>
        /// <returns></returns>
        public static bool AsyncPost(string url, HttpParams urlParameters, int timeout, AsyncHttpCallback callback)
        {
            var sync = new AsyncHttp(timeout);
            return sync.HttpPost(url, urlParameters, callback);
        }

        /// <summary>
        ///  异步：Post方法请求并返回消息 ,可配超时
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <param name="timeout"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static bool AsyncPost(string url, string postData, int timeout, AsyncHttpCallback callback)
        {
            var sync = new AsyncHttp(timeout);
            return sync.HttpPost(url, postData, callback);
        }


        #endregion


    }


}